<template>
  <div class="app-container">
    <!-- 搜索条件 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="游戏名" prop="gameName">
        <el-input v-model="queryParams.gameName" placeholder="请输入游戏名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="自动派单" prop="autoDispatch">
        <el-select v-model="queryParams.autoDispatch" placeholder="请选择自动派单" clearable>
          <el-option v-for="dict in dict.type.auto_dispatch" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="派单间隔" prop="dispatchInterval">
        <el-select v-model="queryParams.dispatchInterval" placeholder="请选择派单间隔" clearable>
          <el-option v-for="dict in dict.type.dispatch_interval" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="消息类型" prop="msgType">
        <el-select v-model="queryParams.msgType" placeholder="请选择消息类型" clearable>
          <el-option
            v-for="dict in dict.type.dispatch_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
            :disabled="dict.value === 'markdown'"  <!-- 禁用markdown -->
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['order:auto:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['order:auto:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['order:auto:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['order:auto:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 表格 -->
    <el-table v-loading="loading" :data="autoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="游戏名" align="center" prop="gameName" />
      <el-table-column label="自动派单" align="center" prop="autoDispatch">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.auto_dispatch" :value="scope.row.autoDispatch"/>
        </template>
      </el-table-column>
      <el-table-column label="派单间隔" align="center" prop="dispatchInterval">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.dispatch_interval" :value="scope.row.dispatchInterval"/>
        </template>
      </el-table-column>
      <el-table-column label="消息类型" align="center" prop="msgType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.dispatch_type" :value="scope.row.msgType"/>
        </template>
      </el-table-column>

      <!-- 改造后的机器人地址列 -->
      <el-table-column label="机器人地址" align="center" prop="webhookUrls" min-width="200">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" :content="scope.row.webhookUrls" placement="top">
            <span class="ellipsis">{{ scope.row.webhookUrls }}</span>
          </el-tooltip>
        </template>
      </el-table-column>

      <el-table-column label="抽成" align="center" prop="userPriceRatio" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['order:auto:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['order:auto:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="游戏名" prop="gameName">
          <el-select v-model="form.gameName" placeholder="请选择游戏">
            <el-option v-for="item in gameOptions" :key="item" :label="item" :value="item"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="自动派单" prop="autoDispatch">
          <el-select v-model="form.autoDispatch" placeholder="请选择自动派单">
            <el-option v-for="dict in dict.type.auto_dispatch" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)" />
          </el-select>
        </el-form-item>
        <el-form-item label="派单间隔" prop="dispatchInterval">
          <el-select v-model="form.dispatchInterval" placeholder="请选择派单间隔">
            <el-option v-for="dict in dict.type.dispatch_interval" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)" />
          </el-select>
        </el-form-item>
        <el-form-item label="消息类型" prop="msgType">
          <el-select v-model="form.msgType" placeholder="请选择消息类型">
            <el-option
              v-for="dict in dict.type.dispatch_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
              :disabled="dict.value === 'markdown'"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="抽成" prop="userPriceRatio">
          <el-input v-model="form.userPriceRatio" placeholder="请输入抽成" />
        </el-form-item>
<!--        <el-form-item label="备注" prop="remark">-->
<!--          <el-input v-model="form.remark" placeholder="请输入备注" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAuto, getAuto, delAuto, addAuto, updateAuto, listGames } from "@/api/order/auto"

export default {
  name: "Auto",
  dicts: ['dispatch_interval', 'auto_dispatch', 'dispatch_type'],
  data() {
    return {
      gameOptions: [],
      loading: true,
      ids: [],
      single: true,
      multiple: true,
      showSearch: true,
      total: 0,
      autoList: [],
      title: "",
      open: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        gameName: null,
        autoDispatch: null,
        dispatchInterval: null,
        msgType: null,
      },
      form: {},
      rules: {
        gameName: [{required: true, message: "游戏名不能为空", trigger: "blur"}],
        autoDispatch: [{required: true, message: "自动派单不能为空", trigger: "change"}],
        dispatchInterval: [{required: true, message: "派单间隔不能为空", trigger: "change"}],
        msgType: [{required: true, message: "消息类型不能为空", trigger: "change"}]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      listAuto(this.queryParams).then(response => {
        this.autoList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    cancel() {
      this.open = false
      this.reset()
    },
    reset() {
      this.form = {
        id: null,
        userId: null,
        gameName: null,
        autoDispatch: null,
        dispatchInterval: null,
        msgType: null,
        webhookUrls: null,
        userPriceRatio: null,
        createTime: null,
        updateTime: null,
        remark: null
      }
      this.resetForm("form")
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    handleAdd() {
      this.reset()
      listGames().then(response => {
        this.gameOptions = response.data
      })
      this.open = true
      this.title = "添加自动派单"
    },
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getAuto(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改自动派单"
      })
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAuto(this.form).then(() => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addAuto(this.form).then(() => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除自动派单编号为"' + ids + '"的数据项？')
        .then(() => delAuto(ids))
        .then(() => {
          this.getList()
          this.$modal.msgSuccess("删除成功")
        }).catch(() => {
      })
    },
    handleExport() {
      this.download('order/auto/export', {...this.queryParams}, `auto_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
<style scoped>
.ellipsis {
  display: inline-block;
  max-width: 180px; /* 可根据列宽调整 */
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  vertical-align: middle;
}

</style>
